<template>
  <div>
    <router-link class="header-abs" tag="div" to="/" v-show="showAbs">
      <div class="iconfont header-abs-back">&#xe624;</div>
    </router-link>
    <div class="header-fixed" v-show="!showAbs" :style="opacityStyle">
      <router-link class="header-fixed-back"  tag="div" to="/">
        <div class="iconfont back-icon">&#xe624;</div>
      </router-link>
      <a class="header-fixed-title">景点详情</a>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showAbs: true,
      opacityStyle: 0
    }
  },
  methods: {
    handleScroll () {
      const top = document.documentElement.scrollTop
      if (top > 60 && top < 140) {
        const opacity = top / 140
        this.opacityStyle = {
          opacity: opacity
        }
        this.showAbs = false
      } else if (top > 140) {
        this.showAbs = false
      } else {
        this.showAbs = true
      }
    }
  },
  activated () {
    window.addEventListener('scroll', this.handleScroll)
  },
  deactivated () { // 防止跳转到别的页面，同样执行 window 的监控程序
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .header-abs
    position: absolute
    left: .2rem
    top: .2rem
    width: .8rem
    height: .8rem
    border-radius: .4rem
    background: rgba(0, 0, 0, .8)
    .header-abs-back
      color: #fff
      line-height: .8rem
      text-align: center
      font-size: .4rem
  .header-fixed
    z-index: 2
    position: fixed
    top:0
    left:0
    right:0
    display:flex
    text-align:center
    line-height: .86rem
    background:$bgColor
    color:#fff
    .header-fixed-back
      position: absolute
      width:.64rem
      float:left
      left: 0
      top: 0
    .header-fixed-title
      width:100%
      color: #fff
</style>
